Skip to content

Preserve indexed coordinates with differing names in to_dataframe (#10851) - #11935

Open
uvcode01-max wants to merge 2 commits into
pydata:mainfrom
uvcode01-max:fix/10851-to-dataframe-include-index-coords
Open

uvcode01-max wants to merge 2 commits into
pydata:mainfrom
uvcode01-max:fix/10851-to-dataframe-include-index-coords

Conversation

@uvcode01-max

Copy link
Copy Markdown
Contributor

Description

Fixes a regression from #10671 where Dataset.to_dataframe() previously excluded all variables present in self.xindexes from the output DataFrame columns (k not in self.xindexes), regardless of whether those variables actually become part of the DataFrame index.

When a coordinate has an index whose name differs from its dimension (e.g. created via .set_xindex("pf") on dimension pos), the DataFrame index is constructed from the dimension (pos), but pf was excluded from columns_in_order and silently lost.

Following the architecture guidance from @headtr1ck in #11025:
The column exclusion now only filters out the dimensions and the levels that actually form the DataFrame index:

index_names = set(ordered_dims) | set(
    self.coords.to_index([*ordered_dims]).names
)
columns_in_order = [k for k in self.variables if k not in index_names]

This preserves indexed coordinates with differing names (e.g. pf) as columns while correctly excluding index levels in MultiIndex scenarios.

Checklist

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
    • I take responsibility for any AI-generated content in my PR.
      Tools: AI assistant used to locate the regression and format test cases.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dataset Index not included as DataFrame column in ._to_dataframe() when name different from dimension name

1 participant